package com.fingard.dsp.bank.directbank.hebbank01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HEBBANK01Balance extends HEBBANK01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = null;
        try {
            retAts = new RetAts7506BalQry(p_req7506.reqHeader);
            retAts.writeStart();
            HEBBANK01Header bankHeader = new HEBBANK01Header();
            bankHeader.bsnCode = "CBE001";
            bankHeader.serialNo = p_req7506.reqHeader.reqSeqID;
            //新建发送银行报文
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("ReqParam");
            xmlWriter.writeElementString("reqNum", String.valueOf(p_req7506.detailCount));
            xmlWriter.writeStartElement("list");
            List<String> actList = new ArrayList<String>();
            while (p_req7506.readRecord()) {
                actList.add(p_req7506.readedDetail.actNum);
                xmlWriter.writeStartElement("row");
                xmlWriter.writeElementString("Account_num", p_req7506.readedDetail.actNum);
                xmlWriter.writeElementString("Account_cur", convertCurToBank(p_req7506.readedDetail.actCur));
                xmlWriter.writeElementString("ReqReserved1", "");
                xmlWriter.writeElementString("ReqReserved2", "");
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();

            String[] result = sendToBank(xmlWriter.toXmlString(), bankHeader);
            if (result[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/SCCBEBankData/opRep/retCode");
                    if ("0000".equals(tmpRetCode)) {
                        Map<String, RetAtsBalDetail> respInfoMap = new HashMap();
                        while (tmpBkReader.read("/SCCBEBankData/opResult/list/row")) {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = "查询成功";
                            tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("Account_num");
                            tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("Balance");
                            tmpRetDetail.custName = tmpBkReader.readedXElement.getSingleText("Acc_name");
                            tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("Usable_balance");
                            tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("Account_cur"));
                            respInfoMap.put(tmpRetDetail.actNum, tmpRetDetail);
                        }
                        //返回查询到的每个账号的信息
                        for (String actNum : actList) {
                            if (respInfoMap.get(actNum) == null) {
                                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                                tmpRetDetail.respCode = "-1";
                                tmpRetDetail.respInfo = "查询失败";
                                tmpRetDetail.actNum = actNum;
                                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                            } else {
                                retAts.writeRetDetail(respInfoMap.get(actNum), p_req7506.isBatch);
                            }
                        }
                        retAts.writeRespCode("0");
                        retAts.writeRespInfo("查询成功！");
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpBkReader.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(result[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            WriteBankLogLn("交易失败：发送异常" + e.getMessage());
            e.printStackTrace();
            return retAts;
        }
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
